package com.education.music.adapter;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.education.music.R;
import com.education.music.entity.OnLineDes;
import com.education.music.http.HttpDownloader;
import com.education.music.weight.DialogLoadding;

import java.io.File;
import java.util.List;

/**
 * 作者：created by albert on 2019-04-23 16:29
 * 邮箱：lvzhongdi@icloud.com
 *
 * @param
 **/
public class OnLineAdapter extends BaseAdapter {

    private Context context;
    private List<OnLineDes> list;
    private LayoutInflater mInflater;
    private DialogLoadding mLoadding;

    public OnLineAdapter(Context context, List<OnLineDes> list) {
        this.context = context;
        this.list = list;
        mInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder mViewHolder;
        if (convertView == null) {
            mViewHolder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.item_online_music, parent, false);
            mViewHolder.mImageView = convertView.findViewById(R.id.item_download_music);
            mViewHolder.mMusicAuthor = convertView.findViewById(R.id.item_download_music_author);
            mViewHolder.mMusicName = convertView.findViewById(R.id.item_download_music_name);
            convertView.setTag(mViewHolder);
        } else {
            mViewHolder = (ViewHolder) convertView.getTag();
        }
        //todo 填充数据

        OnLineDes onLineDes = list.get(position);

        mViewHolder.mImageView.setOnClickListener(v -> {
            mLoadding = new DialogLoadding(context);
            mLoadding.showDialog();
            new downloadMP3Thread(onLineDes.getMusicUrl(), onLineDes.getMusicName()).start();
        });

        mViewHolder.mMusicName.setText(onLineDes.getMusicName());
        mViewHolder.mMusicAuthor.setText(onLineDes.getMusicAuthor());


        return convertView;
    }

    private static class ViewHolder {
        ImageView mImageView;
        TextView mMusicName;
        TextView mMusicAuthor;
    }

    class downloadMP3Thread extends Thread {

        private String url;
        private String musicName;


        public downloadMP3Thread(String url, String musicName) {
            this.url = url;
            this.musicName = musicName;
        }

        public void run() {
            HttpDownloader httpDownloader = new HttpDownloader();
            int downloadResult = httpDownloader.downloadFiles(
                    url, "BoBoMusic", musicName + ".mp3");
            if (downloadResult == 0) {
                context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(new File("/sdcard/BoBoMusic"))));
                Toast.makeText(context, "下载完成", Toast.LENGTH_SHORT).show();
            }else {
                Toast.makeText(context, "下载失败", Toast.LENGTH_SHORT).show();
            }
            mLoadding.closeDialog();
        }
    }
}
